package pwd.allen.androiddemo.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import pwd.allen.androiddemo.R;
import pwd.allen.androiddemo.adapter.PlanetBaseAdapter;
import pwd.allen.androiddemo.entity.Planet;
import pwd.allen.androiddemo.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 下拉框Spinner
 * 适配器
 *  ArrayAdapter：一个条目只能指定一个数据
 *  SimpleAdapter：一个条目能指定多个数据
 */
public class SpinnerActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, View.OnClickListener {

    private List<Planet> planetList;
    private Spinner sp_planet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_adapter);
        // 获取默认的行星列表，即水星、金星、地球、火星、木星、土星
        planetList = Planet.getDefaultList();
        findViewById(R.id.button).setOnClickListener(this);

        //<editor-fold desc="array适配器">
        Spinner sp_array = findViewById(R.id.sp_array);
        List<String> listName = new ArrayList<>();
        for (Planet planet : planetList) {
            listName.add(planet.name);
        }
        sp_array.setAdapter(new ArrayAdapter<String>(this, R.layout.item_select, listName));
        sp_array.setSelection(0);
        // dialog模式下可以设置标题
        sp_array.setPrompt("选择星球");
        sp_array.setOnItemSelectedListener(this);
        //</editor-fold>

        //<editor-fold desc="simple适配器">
        Spinner sp_simple = findViewById(R.id.sp_simple);
        // 声明一个映射对象的列表，用于保存行星的图标与名称配对信息
        List<Map<String, Object>> listMap = new ArrayList<>();
        // iconArray是行星的图标数组，starArray是行星的名称数组
        for (Planet planet : planetList) {
            Map<String, Object> item = new HashMap<>();
            item.put("icon", planet.image);
            item.put("name", planet.desc);
            listMap.add(item);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listMap,
                R.layout.item_simple,
                new String[]{"icon", "name"},
                new int[]{R.id.iv_icon, R.id.tv_name});
        sp_simple.setAdapter(simpleAdapter);
        sp_simple.setSelection(0);
        sp_simple.setOnItemSelectedListener(this);
        //</editor-fold>

        //<editor-fold desc="base适配器">
        sp_planet = findViewById(R.id.sp_base);
        PlanetBaseAdapter planetBaseAdapter = new PlanetBaseAdapter(this, planetList);
        sp_planet.setAdapter(planetBaseAdapter);
        sp_planet.setSelection(0);
        sp_planet.setOnItemSelectedListener(this);
        //</editor-fold>
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        ToastUtil.show(this, "您选择的是" + planetList.get(position).name);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }

    @Override
    public void onClick(View v) {
        // 测试下动态往Adapter的数组里放元素，结果下拉框会动态渲染
        planetList.add(new Planet(R.drawable.apple, "苹果", "一天一个苹果，医生远离我"));
        // 修改了数据需要通知adapter，否则可能报错：The content of the adapter has changed but ListView did not receive a notification
        PlanetBaseAdapter.class.cast(sp_planet.getAdapter()).notifyDataSetChanged();
    }
}